package main

import(
	"os"
	"io/ioutil"
	"log"
	"encoding/json"
	"fmt"
	"os/exec"
)

func main(){
	confFile,err:=os.Open("proc_cleaner.conf")
	if err!=nil{
		log.Fatal("init config file error,",err)
	}
	conf,errConf:=ioutil.ReadAll(confFile)
	if errConf!=nil{
		log.Fatal("read config file error,",errConf)
	}
	confJson:=make(map[string]interface{})
	errConfJson:=json.Unmarshal(conf,&confJson)
	if errConfJson!=nil{
		log.Fatal("parse config file error,",errConfJson)
	}
	pa:=new(os.ProcAttr)
	pa.Files = []*os.File{nil, os.Stdout, os.Stderr} 
	p,err1:=os.StartProcess(fmt.Sprintf("%s",confJson["runProcName"]),nil,pa)
	if err1!=nil{
		log.Fatal("start process error,",err1)
	}
	fmt.Println("waiting for ",confJson["runProcName"],"exit ")
	_,err2:=p.Wait()
	if err2!=nil{
		log.Fatal("wait process exit error,",err2)
	}
	killProcNames:=confJson["killProcNames"].([]interface{})
	for i:=0;i<len(killProcNames);i++{
		exec.Command("cmd.exe","/c"," taskkill /im "+killProcNames[i].(string)).Run()
	}
	println("done...")
}